package com.linln.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linln.common.data.MybatisPage;
import com.linln.component.shiro.ShiroUtil;
import com.linln.modules.business.dao.MapperTask;
import com.linln.modules.business.domain.Task;
import com.linln.modules.business.dto.MyTaskDTO;
import com.linln.modules.system.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 工作任务服务
 *
 * @author chaunceyzhong
 */
@Service
public class TaskServiceImpl extends ServiceImpl<MapperTask, Task> {


    @Autowired
    MapperTask mapperTask;

    /**
     * 获取我的申请任务列表
     *
     * @return
     */
    public IPage<MyTaskDTO> getMyReqTask() {
        IPage page = MybatisPage.pageRequest();
        Long userId = ShiroUtil.getSubject().getId();
        IPage<MyTaskDTO> myReqTask = mapperTask.getMyReqTask(page, userId);
        return myReqTask;
    }

    /**
     * 开启任务
     *
     * @param businessKey
     * @param user
     * @return
     */
    public boolean startTask(Long businessKey, Integer taskType,User user) {
        Task task = new Task();
        //普通管理员ID
        task.setAuditor(8L);
        task.setBusinessKey(businessKey);
        task.setCreateUserId(user.getId());
        task.setCreateUserName(user.getUsername());
        task.setType(taskType);
        //开启新任务 设置状态为审批中
        task.setStatus(0);
        task.setCreateTime(new Date());
        task.setUpdateTime(new Date());
        boolean b = this.save(task);
        return b;
    }

    /**
     * 完成任务
     * @param taskId
     * @param status
     * @return
     */
    public boolean completeTask(Long taskId, Integer status) {
        UpdateWrapper<Task> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", status);
        updateWrapper.set("update_time",new Date());
        updateWrapper.eq("id", taskId);
        return this.update(updateWrapper);
    }

}
